package com.orange.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.commonutils.result.Result;
import com.orange.eduservice.entity.EduTeacher;
import com.orange.eduservice.query.TeacherQuery;
import com.orange.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author orange
 * @since 2021-12-12
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("eduService/teacher")
@CrossOrigin    //开启跨域访问
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有讲师
     */
    @ApiOperation("查询所有讲师")
    @GetMapping("/list")
    public Result listTeachers() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return Result.ok().data("teachers", list);
    }

    /**
     * 根据id逻辑删除讲师
     */
    @ApiOperation("根据id删除讲师")
    @DeleteMapping("/delete/{id}")
    public Result removeTeacher(
            @ApiParam(name = "id", value = "讲师id", required = true)
            @PathVariable String id) {
        eduTeacherService.removeById(id);
        return Result.ok();
    }

    /**
     * 分页查询讲师
     */
    @ApiOperation("分页查询讲师")
    @PostMapping("/pageList/{page}/{limit}")
    public Result pageList(@ApiParam(name = "page", value = "当前页码", required = true)
                           @PathVariable Long page,
                           @ApiParam(name = "limit", value = "每页记录数", required = true)
                           @PathVariable Long limit,
                           @ApiParam("查询对象\r\r\tbegin:起始时间\r\r\tend:结束时间\r\r\tlevel:讲师等级\r\r\tname:讲师姓名")
                           @RequestBody TeacherQuery teacherQuery) {

        Page<EduTeacher> pageParam = new Page<>(page, limit);
        eduTeacherService.pageQuery(pageParam, teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return Result.ok().data("total", total).data("teachers", records);
    }

    /**
     * 添加讲师
     */
    @ApiOperation("新增讲师")
    @PostMapping("/add")
    public Result addTeacher(@ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
                             @RequestBody EduTeacher eduTeacher) {
        eduTeacherService.save(eduTeacher);
        return Result.ok();
    }

    /**
     * 通过id查询讲师
     */
    @ApiOperation("根据id查询讲师")
    @GetMapping("select/{id}")
    public Result getById(@ApiParam(name = "id", value = "讲师id", required = true)
                          @PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
        /*//异常处理测试
        try {
            int a = 10 / 0;
        }catch (Exception e){
            throw new MyException(1001,"执行了自定异常");
        }*/
        return Result.ok().data("teacher", teacher);
    }

    /**
     * 修改讲师
     */
    @ApiOperation("修改讲师")
    @PutMapping("update")
    public Result updateById(@ApiParam(name = "eduTeacher", value = "讲师对象", required = true)
                             @RequestBody EduTeacher eduTeacher) {
        //eduTeacher.setId(id);
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }
}

